﻿using System;

namespace Layers.History
{
    public class BitBuffer
    {
        private byte[] _data;
        private int _length;

        public BitBuffer(int bitCapacity)
        {
            _data = new byte[bitCapacity / 8 + 1];
            _length = 0;
        }

        public int BitLength { get { return _length; } }

        public int ByteLength { get { return _data.Length; } }

        public int WriteBits(int bitOffset, int value, int count)
        {
            EnsureCapacity(bitOffset + count);
            return bitOffset + count;
        }

        public int ReadBits(int bitOffset, int count)
        {
            return 0;
        }

        private void EnsureCapacity(int bitCapacity)
        {
            if (_data.Length * 8 < bitCapacity)
            {
                byte[] newdata = new byte[bitCapacity * 2];
                Array.Copy(_data, newdata, _data.Length);
                _data = newdata;
            }
        }
    }



    public class BitStream
    {
        private BitBuffer _buffer;
        private int _bitOffset;

        public BitStream(BitBuffer buffer)
        {
            _buffer = buffer;
            _bitOffset = 0;
        }

        public int BitOffset { get { return _bitOffset; } }

        public void Write(int code, int count)
        {
            _bitOffset = _buffer.WriteBits(_bitOffset, code, count);
        }

        public int Read(int count)
        {
            _bitOffset = _buffer.ReadBits(_bitOffset, count);
            return 0;
        }
    }

    
}